package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Article;
import org.rcloud.io.model.ArticleTable05Mapping;
import org.rcloud.io.model.Rural;
import org.rcloud.io.model.RuralSpecialtyMapping;
import org.rcloud.io.service.ArticleService;
import org.rcloud.io.service.RuralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/rural")
public class RuralController {

    @Autowired
    private RuralService ruralService;

    @Autowired
    private ArticleService articleService;

    /*
  * 获取所有的田园餐桌信息
  * */
    @ApiOperation(value="获取所有的田园餐桌信息", notes="获取所有的田园餐桌信息")
    @ResponseBody
    @RequestMapping(value = "/findRural", produces = {"application/json;charset=UTF-8"})
    public PageVo<Rural> findRural(){
        return ruralService.findRural();
    }

    /*
  * 根据eid获取田园餐桌信息
  * */
    @ApiOperation(value = "根据eid获取田园餐桌信息", notes = "根据eid获取田园餐桌信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo findByEid(String eid) {
        return ruralService.findByEid(eid);
    }

    /*
    * 保存田园餐桌信息
    * */
    @ApiOperation(value = "保存田园餐桌信息", notes = "保存田园餐桌信息")
    @ResponseBody
    @RequestMapping(value = "/saveRural", produces = {"application/json;charset=UTF-8"})
    public boolean saveRural(@RequestBody String params) {

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        String nameTitle = name+System.currentTimeMillis();//生成时间戳,确保两张表能够关联

        Rural rural = new Rural();
        rural.setEid(eid);
        rural.setName(name);
        rural.setAddress(address);
        rural.setLongitude(longitude);
        rural.setLatitude(latitude);
        rural.setEtype(etype);
        rural.setSummary(summary);
        rural.setReport(report);
        rural.setUsername(username);
        rural.setUserphone(userphone);
        rural.setInf02(nameTitle);
        rural.setFlag(1);

        Article article = new Article();
        article.setTitle(nameTitle);

        int end = Integer.valueOf(ruralService.saveRural(rural).getInfo().toString());
        if(end!=1){
            return false;
        }

        //保存文章
        int art = Integer.valueOf(articleService.save(article).getInfo().toString());
//        System.out.println("将文章的数据保存,状态为:"+art);
        if(art!=1){
            return false;
        }

        String INF01 = articleService.findTitle(nameTitle).getInfo().toString();
//        System.out.println("查找文章的id为="+shouming);

        int upd = Integer.valueOf(ruralService.updateArtid(nameTitle,INF01).getInfo().toString());
//        System.out.println("修改刚刚保存的信息,就是将文章id放代言家庭到表中,返回的状态为="+upd);
        if (upd!=1){
            return false;
        }

        return true;
    }

    /*
   * 根据id获取田园餐桌信息
   * */
    @ApiOperation(value = "根据id获取田园餐桌信息", notes = "根据id获取田园餐桌信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(Integer id) {
       /* String[] ids = params.split("=");
        int id = Integer.parseInt(ids[1]);*/
        return ruralService.findById(id);
    }

    /*
     * 修改田园餐桌信息
   c  * */
    @ApiOperation(value="修改田园餐桌信息", notes="修改田园餐桌信息")
    @ResponseBody
    @RequestMapping(value = "/updateRural", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateRural(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Rural rural = new Rural();
        rural.setId(id);
        rural.setEid(eid);
        rural.setName(name);
        rural.setAddress(address);
        rural.setLongitude(longitude);
        rural.setLatitude(latitude);
        rural.setEtype(etype);
        rural.setSummary(summary);
        rural.setUsername(username);
        rural.setUserphone(userphone);

        return ruralService.updateRural(rural);
    }

    @ApiOperation(value = "添加餐桌、特色菜关联", notes = "添加餐桌、特色菜关联")
    @ResponseBody
    @RequestMapping(value = "/savemapping", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(Integer rId, Integer sId) {
        RuralSpecialtyMapping articleTable011Mapping = new RuralSpecialtyMapping();
        articleTable011Mapping.setrId(rId);
        articleTable011Mapping.setsId(sId);
        return ruralService.saveMapping(articleTable011Mapping);
    }

    @ApiOperation(value="田园餐桌地点绑定", notes="田园餐桌地点绑定")
    @ResponseBody
    @RequestMapping(value = "/binding", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResultVo binding(Integer aId, Integer tId) {
        ArticleTable05Mapping articleTable011Mapping = new ArticleTable05Mapping();
        articleTable011Mapping.setaId(aId);
        articleTable011Mapping.settId(tId);
        return ruralService.saveSite(articleTable011Mapping);
    }

    /**
     * 分页
     */
    @ApiOperation(value="田园模糊分页", notes="田园模糊分页")
    @ResponseBody
    @RequestMapping(value = "/selectByKey", produces = {"application/json;charset=UTF-8"})
    public PageInfo<Rural> selectByKey(@RequestBody String params){
        JSONObject  object = JSONObject.parseObject(params);
        Map map = JSONObject.parseObject(object.get("params").toString());
        int pageNum = Integer.parseInt(object.get("pageNum").toString());
        int pageSize = Integer.parseInt(object.get("pageSize").toString());
        PageInfo<Rural> pageInfo = ruralService.selectByKey(pageNum,pageSize,map);
        return pageInfo;
    }
}
